<?php

require_once("cfg/def.php");
require_once("cfg/cfg.php");
require_once("lib/logger.class.php");
require_once("lib/err.package.php");
require_once("lib/localestrings.class.php");
require_once("lib/asterisk.class.php");
require_once("models/caller.class.php");

$eHandler = new exceptionErrorHandler();
$eHandler->error_formatter = new SimpleFormatter();

Logger::$log_file = USR_LOG_FILE;

header('Content-Type: text/plain; charset=utf-8');

Logger::Write("call.php GET", print_r($_GET, true));
Logger::Write("call.php POST", print_r($_POST, true));

$lang = 'ru';
if(isset($_GET['lang']) && (strtolower($_GET['lang']) == 'en')) $lang = 'en';
$loc = new LocaleStrings($lang, 'call-states');

define("ACT_ID", "IDENTIFY");

$result = array("STATE" => "null", "MSG" => "null", "DESC" => "nothing");

try
{
	$act = strtoupper(get_item($_GET, "act", ACT_ID));
	$secs = get_item($_GET, "timeout", CALLWAIT_TIMEOUT_SEC);
	Logger::Write("call.php act", $act);
	$phone = get_item($_GET, "phone", null);
	switch($act)
	{
		case ACT_ID:
			if(is_null($phone) || !is_numeric($phone))
			{
				$code = '400';
				$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
			}
			else
			{
				$caller = new Caller($phone);
				$caller->reg_http();
				
				$code = ($caller->wait($secs)) ? '200' : '408';
				$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
			}
			break;

		case "CALL":
			if(is_null($phone))
			{
				$code = '400';
				$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
			}
			else
			{
				$caller = new Caller($phone);
				$expected = $caller->reg_call();
				$code = $expected ? '200' : '404';
				$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
			}
			break;
		
		case "SMS":
			$msg = urldecode(get_item($_GET, "msg", null));
			Logger::Write("call.php SMS", "#".$phone.": ".$msg);
			
			if (strlen($msg) > 0)
			{
				$a = new Asterisk();
				$a->connect(AST_ADDR, AST_PORT, "admin", "1234");
				$buf = $a->sendSMS($phone, $msg);
				$a->disconnect();
				Logger::Write("call.php sendSMS", $buf);
			}
			break;

		case "IDENTIFY2":
			$data = get_item($_POST, 'req', '');
			$req = json_decode($data);
			
			// Переменная $phone используется для подстановки в ответ
			$phone = $req->ring->phone;
			$caller = new Caller($phone);
			$caller->reg_http();
			
			$code = ($caller->wait($req->ring->timeout)) ? '200' : '408';
			$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
				
			break;

		case "IDENTIFY3":
			$data = get_item($_POST, 'req', '');
			$req = simplexml_load_string($data);
			
			// Переменная $phone используется для подстановки в ответ
			$phone = $req->ring['phone'];
			$caller = new Caller($phone);
			$caller->reg_http();
			
			$code = ($caller->wait($req->ring['timeout'])) ? '200' : '408';
			$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
				
			break;

		case "REQUEST":
			$result = array("CODE" => "200", "MSG" => "+79123456789");
			break;

		default:
			$code = '406';
			$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
			break;
	}
}
catch(Exception $e)
{
	Logger::Write("call.php", $e);
	$code = '406';
	$result = array("CODE" => $code, "MSG" => $loc->gets('state', $code));
}

$result['MSG'] = str_replace('{phone}', $phone, $result['MSG']);

echo json_encode($result);


function get_item($items, $key, $def = null)
{
	if(is_null($items)) return $def;
	if(!is_array($items)) return $def;
	if(isset($items[$key]))
		return $items[$key];
	else
		return $def;
}
